package com.ld.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @Author: Liudao
 * @Date: 2022-08-17-12:25
 * @Description:
 */
@Controller
@RequestMapping("/user")
public class UserController {

@RequestMapping("login")
    public String login(String username, String password, HttpSession session){
//    判断用户名密码是否正确
    if ("666".equals(username) && "123".equals(password)){
        //满足就把username添加到session
        session.setAttribute("username",username);
        //并重定向到search请求页面
        return "redirect:/emp2/search.do";
    }//否则返回到本页面
    return "redirect:/login.html";
}

//进行文件上传（同步表单上传）
@RequestMapping("upload")
    public String upload(HttpServletRequest request, MultipartFile mfile) throws IOException {
//    得到上传文件的真实名称
    String originalFilename = mfile.getOriginalFilename();
//取得文件后缀名
    String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
//使用uuid生成主文件名
    String filename = UUID.randomUUID().toString().substring(0,5);
//进行文件上传
//    得到/upload的真实路径
    //得到application对象
    ServletContext servletContext = request.getServletContext();
    //得到webapp下upload的绝对路径
    String realPath = servletContext.getRealPath("/upload");
    //构造要上传的文件
    File file = new File(realPath + "/" + filename + substring);
    //进行上传
    mfile.transferTo(file);
    //返回逻辑视图
    return "/upload/list";
}

//异步文件上传


}
